package com.play.ballen.me.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.play.ballen.R

class PrivacyDialogUtils(private val mContext: Context) {

    val dialog: Dialog = Dialog(mContext)
    private lateinit var contentView: View
    private lateinit var tvDesc: TextView

    fun show(text: String) {
        if (!this::contentView.isInitialized) {
            contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_privacy, null)
            contentView.let {
                tvDesc = it.findViewById(R.id.tv_dialog_message)

                it.findViewById<ImageView>(R.id.imgClose).setOnClickListener { dialog.dismiss() }

                dialog.setContentView(it)
                dialog.setCanceledOnTouchOutside(true)
                dialog.window?.setLayout(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
                dialog.window?.setBackgroundDrawable(ColorDrawable())
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            tvDesc.text = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)
        } else {
            tvDesc.text = Html.fromHtml(text)
        }

        if (!dialog.isShowing) {
            dialog.show()
        }
    }
}